<?php
include_once(DB_FILE);

$MAX_ROW=52; //numero colenne in table in DB
$PARSER=array('/C;Y[0-9]*;X[0-9]*;K/',
		'/ID;PGEO&SOFT/',
		'/E/',
		'/"/');
$QUOTE_ASSOLUTE=array(
    "PZ01" =>2.55,"PZ02" =>3.56,"PZ03" =>3.61,"PZ04" =>4.16,"PZ05" =>4.21,"PZ06" =>-1.42,"PZ07" =>1.09,"PZ08" =>1.19,"PZ09" =>0.11,"PZ10" =>0.21,"PZ11" =>-2.02,"PZ12" =>2.54,
    "PZ13" =>2.64,"PZ14" =>-4.34,"PZ15" =>1.2,"PZ16" =>1.3,"PZ17" =>-2.59,"PZ18" =>1.66,"PZ19" =>1.76,"PZ20" =>-0.85,"PZ21" =>0.87,"PZ22" =>0.97,"PZ23" =>-2.84,"PZ24" =>-6.11,
    "PZ25" =>2.59,"PZ26" =>-4.72,"PZ27" =>-4.27,"PZ28" =>1.83,"PZ29" =>-1.6,"PZ30" =>-0.54,"PZ31" =>-5.66,"PZ32" =>-5.06,"PZ33" =>3.14,"PZ34" =>-1.17,"PZ35" =>0.33,"PZ36" =>3.43,
    "PZ37" =>-4.06);
$QUOTE_ASSOLUTE=array(2.55,
0,
3.56,
3.61,
0,
4.16,
4.21,
0,
-1.42,
1.09,
1.19,
0,
0.11,
0.21,
0,
-2.02,
2.54,
2.64,
0,
-4.34,
1.2,
1.3,
0,
-2.59,
1.66,
1.76,
0,
-0.85,
-0.87,
-0.97,
0,
-2.84,
-6.11,
2.59,
0,
-4.72,
-4.27,
1.83,
0,
-1.6,
-0.54,
0,
-5.66,
-5.06,
3.14,
0,
-1.17,
0.33,
3.43,
0,
-4.06,
0);

function cleanPars($str)
{
	global $PARSER;
	$string=preg_replace($PARSER, '', $str);
	return $string;
}
function cleanParsFloat($str)
{
	global $PARSER;
	$string=preg_replace($PARSER, '', $str);
	$ret= sprintf("%.7f", $string);
	return $ret;
}
function parserStbFile()
{
	global $PARSER,$MAX_ROW,$DATA_SCHEDULE;

    echo "parser $DATA_SCHEDULE";
    $DATA_SCHEDULE=strftime('%Y-%m-%d %H:%M:%S',time());
    echo "modifica $DATA_SCHEDULE";
    return 0;

	$msql  = new Db;
	$msql->connect();

	$log=new FileLog();
	     
	foreach (glob(STB_DIR."/dati_impianto_ravenna_*.xls") as $filename)
	{
		$log->writeLog( __FUNCTION__." $filename size filesize($filename) ");
	}
	$log->close();	
	
	$str_query="INSERT INTO dati_impianto (data,P1Pz01,P1Rx,P2Pz02,P2Pz03,P2Rx,P3Pz04,P3Pz05,P3Rx,P4Pz06,P4Pz07,P4Pz08,P4Rx,P5Pz09,P5Pz10,P5Rx,P6Pz11,P6Pz12,P6Pz13,P6Rx,P7Pz14,P7Pz15,P7Pz16,P7Rx,P8Pz17,P8Pz18,P8Pz19,P8Rx,P9Pz20,P9Pz21,P9Pz22,P9Rx,P10Pz23,P10Pz24,P10Pz25,P10Rx,P11Pz26,P11Pz27,P11Pz28,P11Rx,P12Pz29,P12Pz30,P12Rx,P13Pz31,P13Pz32,P13Pz33,P13Rx,P14Pz34,P14Pz35,P14Pz36,P14Rx,P15Pz37,P15Rx) VALUES ";
	
	$lines = file($filename); 
	$num_row=count($lines);
	
	for($i=0;$i<$num_row;$i++)
	{ 
		 $string = cleanPars($lines[$i]);
		
		 if(preg_match('/[0-9]{1,}\/[0-9]{1,}\/[0-9]{4}/',$string))
		 {
			$query=$str_query;
			
		 	$data=$string;
			list($d, $m, $y) = explode('/',trim($data));
			list($h,$min,$sec) = explode('.',trim(cleanPars($lines[++$i])));
			$data=mktime($h,$min,$sec,$m,$d,$y);
			$data=strftime('%Y-%m-%d %H:%M:%S',$data);

                        $DATA_SCHEDULE=$data;
			
			$query.="('$data',";
			for($col=0;$col< $MAX_ROW;$col++)
			{
                            $valore_piezo=trim(cleanParsFloat($lines[++$i]));
                            //posizione segnale
                            if(!($col == 1 || $col == 4  || $col == 7  || $col == 11 || $col == 14 || $col == 18 || $col == 22 ||
                              $col == 26 || $col == 30 || $col == 34 || $col == 38 || $col == 41 || $col == 45 || $col == 49 || 
                              $col == 51 ))
                                $valore_piezo=$valore_piezo/100 +($QUOTE_ASSOLUTE[$col]);
                            
                            $query.="'".$valore_piezo."'";
                            if($col < ($MAX_ROW - 1))
                                    $query.=",";
                            else
                                    $query.=");";
			}
			echo "$query\n";
                        $msql->execute($query,false);
		 }
	}
	$msql->disconnettiDB();	
	return 0;
}
?>